package com.kitkat.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kitkat.entities.Project;
import com.kitkat.service.ProjectService;

@Controller
@RequestMapping("/project")
public class ProjectController {
	@Autowired
	private ProjectService projectService;
	
	/*@RequestMapping(value="",method={RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody Map<String,Object> newProject(HttpServletRequest request,String name, String description, String token,@RequestParam(value="file",required=false)MultipartFile file,String templateId){
		Map<String,Object> hm = new HashMap<String,Object>();
		String fileName = file.getOriginalFilename();
		int lastIndexOfDot = fileName.lastIndexOf(".");
		if(lastIndexOfDot==-1){
			hm.put("code",CodeAndMessage.SuccessCode);
			hm.put("message", "背景图片格式有误！");
			hm.put("result", null);
			return hm;
		}
		String sep = "/";
		sep = System.getProperty("file.file.separator");
		MyIcon icon = new MyIcon();
		icon.setName(fileName.substring(0, lastIndexOfDot));
		icon.setSuffix(fileName.substring(lastIndexOfDot+1));
		String uuidName = UUID.fromString(icon.getName()).toString();
		String uploadDir = request.getServletContext().getRealPath("/")+"resources"+sep+"upload";
	    File dirPath = new File(uploadDir);
	    if (!dirPath.exists()) {
	    	dirPath.mkdirs();
	    }
	    String path = uploadDir + sep + uuidName +"."+icon.getSuffix();
	    String urlPath = request.getContextPath()+"/resources/upload/" +"."+icon.getSuffix();
		icon.setPath(path);
		icon.setUrlPath(urlPath);
	    icon.setSubmitDate(new Date());
		
		File uploadedFile = new File(path);
		try {
			FileCopyUtils.copy(file.getBytes(), uploadedFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		Map<String, Object> result = this.projectService.newProject(name, description, token, icon,templateId);
		return result;
	}*/
	
	@RequestMapping(value="",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> newProject(String token,String name, String description,String templateId){
		Map<String, Object> result = this.projectService.newProject(token,name, description, null,templateId);
		return result;
	}
	
	@RequestMapping(value="/id/{id}",method=RequestMethod.GET)
	public @ResponseBody Map<String,Object> getProject(String token,@PathVariable(value="id") String projectId){
		Map<String,Object> result = this.projectService.getProjectByProjectId(token,projectId);
		Object obj = result.get("result");
		Project project = null;
		if(obj!=null){
			project = (Project)obj;
			project.setUser(null);
		}
		result.put("result", project);
		return result;
	}
	
	@RequestMapping(value="/id/{id}",method=RequestMethod.PUT)
	public @ResponseBody Map<String,Object> updateProject(String token,String name,String description,String content,@PathVariable(value="id") String projectId){
		Map<String,Object> result = this.projectService.updateProjectByProjectId(token,name,description,content,projectId);
		return result;
	}
	
	@RequestMapping(value="/id/{id}",method=RequestMethod.DELETE)
	public @ResponseBody Map<String,Object> deleteProject(String token,@PathVariable(value="id") String projectId){
		Map<String,Object> result = this.projectService.deleteProjectByProjectId(token,projectId);
		return result;
	}
}
